package cn.stylefeng.guns.modular.rest.agv;

import cn.stylefeng.guns.modular.agv.model.params.TaskGenAgvHkParam;
import cn.stylefeng.guns.modular.agv.model.params.TaskPassagewayAgvHkParam;
import cn.stylefeng.guns.modular.agv.model.result.TaskGenAgvHkResult;
import cn.stylefeng.guns.modular.agv.model.result.TaskPassagewayAgvHkResult;
import cn.stylefeng.guns.modular.agv.service.TaskGenAgvHkService;
import cn.stylefeng.guns.modular.agv.service.TaskPassagewayAgvHkService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 *地址控制器
 */
@Controller
@RequestMapping("/rest/genAgvSchedulingTask")
public class restGenAgvSchedulingTaskController extends BaseController {

    @Autowired
    private UserAop userAop;

    @Autowired
    private TaskGenAgvHkService taskGenAgvHkService;

    @Autowired
    private TaskPassagewayAgvHkService taskPassagewayAgvHkService;

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-05-14
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TaskGenAgvHkParam taskGenAgvHkParam) {
        User user = userAop.getUser();
        if(user !=null && user.getUserId() != null){
            taskGenAgvHkParam.setCreateUser(user.getUserId());
        }

        if(taskGenAgvHkParam.getClientCode() == null){
            taskGenAgvHkParam.setClientCode("APP");
        }
        // 判断是否重复
        TaskGenAgvHkParam q = new TaskGenAgvHkParam();
        q.setSrcPositionCode(taskGenAgvHkParam.getSrcPositionCode());
        q.setSrcPositionType("00");
        q.setStat("00");
        List<TaskGenAgvHkResult> taskGenAgvHkResults = this.taskGenAgvHkService.taskListByStat(q);
        if(taskGenAgvHkResults != null && taskGenAgvHkResults.size() > 0){
            if(StringUtils.isNotBlank(taskGenAgvHkResults.get(0).getRspMsg())){
                return ResponseData.error(taskGenAgvHkResults.get(0).getRspMsg());
            }else{
                return ResponseData.error("["+taskGenAgvHkParam.getSrcPositionCode()+"]任务已创建！！！");
            }
        }

        this.taskGenAgvHkService.add(taskGenAgvHkParam);
        return ResponseData.success();
    }


    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-05-14
     */
    @ResponseBody
    @RequestMapping("/taskListByStat")
    public List<TaskGenAgvHkResult> taskListByStat(TaskGenAgvHkParam taskGenAgvHkParam) {
        taskGenAgvHkParam.setStat("01");
        return this.taskGenAgvHkService.taskListByStat(taskGenAgvHkParam);
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-05-14
     */
    @RequestMapping("/setPriority")
    @ResponseBody
    public ResponseData setPriority(TaskGenAgvHkParam taskGenAgvHkParam) {
        taskGenAgvHkParam.setSetPriority("1");
        this.taskGenAgvHkService.update(taskGenAgvHkParam);
        return ResponseData.success();
    }


    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-05-16
     */
    @ResponseBody
    @RequestMapping("/clearRoadWay")
    public List<TaskPassagewayAgvHkResult> clearRoadWay(TaskPassagewayAgvHkParam taskPassagewayAgvHkParam) {
        return this.taskPassagewayAgvHkService.findListBySpec(taskPassagewayAgvHkParam);
    }

    @RequestMapping("/setClearRoadWay")
    @ResponseBody
    public ResponseData setClearRoadWay(TaskPassagewayAgvHkParam taskPassagewayAgvHkParam) {
        taskPassagewayAgvHkParam.setOperation("1");
        this.taskPassagewayAgvHkService.update(taskPassagewayAgvHkParam);
        return ResponseData.success();
    }
    
}
